****estimation of later-life shocks
qui{
use "$temp/psid_long_cleaned", clear
ren statefips stfips
keep if age>=36 //limit age range
keep if !coll

****estimation of mean
//deflate by skill prices
ren stfips statefips
merge m:1 statefips using "$temp/skill_prices_all"
keep if _merge == 3
drop _merge deflator
replace wages = wages/skill_price_2000 //in case of a move
replace wages = wages/hours //hourly wage

gen age_group = 1
replace age_group = 2 if age>=54 //two age groups
gen count = 1
collapse (sum) weight count (mean) wages, by(uniqid age_group)
drop if count<6 //require be present for 6 years or more

duplicates tag uniqid, gen(dup)
drop if dup==0 //drop if individual appears in only one age group

//convert to wide form again, in a sense
gen wages_old = .
replace wages_old = wages[_n+1] if age_group[_n] == 1
drop if age_group == 2
drop dup

su wages, d
su wages_old, d
drop if wages>100 | wages_old>100

//geneate difference
gen eps = log(wages_old) - log(wages) 
su eps [w=weight] //and here's the mean! very similar to seshadri and li: 0.039 -- close to zero, but yeah. hourly wages do go up a wee bit
local mean = round(`r(mean)', 0.01)

//variance estimation
gen log_old = log(wages_old)
gen log_young = log(wages)
//su log_old log_young //variance of .0545 --> SD 0f 0.277. reasonable
su log_old [w=weight]
local sd_1 = `r(sd)'
su log_young [w=weight]
local sd_2 = `r(sd)'

local var = `sd_1'^2 - `sd_2'^2
local sd = round(sqrt(`var'), 0.01)

noi di in red "`mean'"
noi di in red "`sd'"
}

****estimation of later-life shocks
qui{
use "$temp/psid_long_cleaned", clear
ren statefips stfips
keep if age>=36 //limit age range
keep if coll

****estimation of mean
//deflate by skill prices
ren stfips statefips
merge m:1 statefips using "$temp/skill_prices_all"
keep if _merge == 3
drop _merge deflator
replace wages = wages/skill_price_coll_2000 //in case of a move
replace wages = wages/hours //hourly wage

gen age_group = 1
replace age_group = 2 if age>=54 //two age groups
gen count = 1
collapse (sum) weight count (mean) wages, by(uniqid age_group)
drop if count<6 //require be present for 6 years or more

duplicates tag uniqid, gen(dup)
drop if dup==0 //drop if individual appears in only one age group

//convert to wide form again, in a sense
gen wages_old = .
replace wages_old = wages[_n+1] if age_group[_n] == 1
drop if age_group == 2
drop dup

su wages, d
su wages_old, d
drop if wages>100 | wages_old>100

//geneate difference
gen eps = log(wages_old) - log(wages) 
su eps [w=weight] //and here's the mean! very similar to seshadri and li: 0.039 -- close to zero, but yeah. hourly wages do go up a wee bit
local mean = round(`r(mean)', 0.01)

//variance estimation
gen log_old = log(wages_old)
gen log_young = log(wages)
//su log_old log_young //variance of .0545 --> SD 0f 0.277. reasonable
su log_old [w=weight]
local sd_1 = `r(sd)'
su log_young [w=weight]
local sd_2 = `r(sd)'

local var = `sd_1'^2 - `sd_2'^2
local sd = round(sqrt(`var'), 0.01)

di in red "`mean'"
di in red "`sd'"
}

